#!/bin/sh

set -e

# Check pre-requisites
if test -z "`which node`"; then
    echo "Error: requires node.js, please install and re-run. Aborting.";
    exit 1;
fi

if test -z "`which npm`"; then
    echo "Error: requires npm, please install and re-run. Aborting.";
    exit 1;
fi

# Get the xterm.js repo and checkout specific commit
if [ ! -d "./xterm.js" ]; then
    git clone git@github.com:sourcelair/xterm.js.git xterm.js
fi

cd xterm.js
git clean -dfx

# xterm version 2.2.0
git checkout 8a146b5

# install and build via Babel
npm install --only=dev
npm run build 

XTERM_BUILD_DIR=./build
XTERM_ADDONS_DIR=./dist/addons
XTERM_TARGET_DIR=../../src/org/rstudio/studio/client/workbench/views/terminal/xterm

# stylesheet
cp ${XTERM_BUILD_DIR}/xterm.css ${XTERM_TARGET_DIR}/xterm.css

# uncompressed js
cp ${XTERM_BUILD_DIR}/xterm.js ${XTERM_TARGET_DIR}/xterm-uncompressed.js
cp ${XTERM_ADDONS_DIR}/fit/fit.js ${XTERM_TARGET_DIR}/fit-uncompressed.js

# minified js using closure
minify () {
  echo "Minifying $1 to $2"
  CC_OPTS="--compilation_level SIMPLE_OPTIMIZATIONS --language_in ECMASCRIPT5"
  java -Xmx128M -jar "../compiler/compiler.jar" $CC_OPTS --js $1 --js_output_file $2
}

minify ${XTERM_BUILD_DIR}/xterm.js ${XTERM_TARGET_DIR}/xterm.js
minify ${XTERM_ADDONS_DIR}/fit/fit.js ${XTERM_TARGET_DIR}/fit.js

echo "Tweaking CSS"
cd src
../../tweak-xterm-css.R
echo "xterm.css adjusted. Be sure to rebuild Ace themes if necessary (compile-themes.R)."
echo Done!
